<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Class	: M_purchasertndet
 * 
 * Table	: purchasertndet
 *  
 * @author Eko Junaidi Salam
 *
 */
class M_purchasertndet extends CI_Model{

	function __construct(){
		parent::__construct();
	}
	
	/**
	 * Fungsi	: getAll
	 * 
	 * Untuk mengambil all-data
	 * 
	 * @param number $start
	 * @param number $page
	 * @param number $limit
	 * @return json
	 */
	function getAll($no_pr,$start, $page, $limit){
		$query  = $this->db->limit($limit, $start)->order_by('ID', 'DESC')->get_where('purchasertndet',array('NOMOR_PURCHASE_RETURN'=>$no_pr))->result();
		$total  = $this->db->get_where('purchasertndet',array('NOMOR_PURCHASE_RETURN'=>$no_pr))->num_rows();
		
		$data   = array();
		foreach($query as $result){
			$data[] = $result;
		}
		
		$json	= array(
						'success'   => TRUE,
						'message'   => "Loaded data",
						'total'     => $total,
						'data'      => $data
		);
		
		return $json;
	}
	
	/**
	 * Fungsi	: save
	 * 
	 * Untuk menambah data baru atau mengubah data lama
	 * 
	 * @param array $data
	 * @return json
	 */
	function save($data){
		$last   = NULL;
		
		$pkey = array('NOMOR_PURCHASE_RETURN'=>$data->NOMOR_PURCHASE_RETURN,'NOMOR_LPB'=>$data->NOMOR_LPB);
		
		if($this->db->get_where('purchasertndet', $pkey)->num_rows() > 0){
			/*
			 * Data Exist
			 */			 
			$tmp = substr($data->HARGA_SATUAN,3,strlen($data->HARGA_SATUAN));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->HARGA_SATUAN = $tmp;$tmp = substr($data->HARGA_TOTAL,3,strlen($data->HARGA_TOTAL));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->HARGA_TOTAL = $tmp;	
			 
			$arrdatau = array('TGL_DETAIL_PURCHASE_RETURN'=>(strlen(trim($data->TGL_DETAIL_PURCHASE_RETURN)) > 0 ? date('Y-m-d', strtotime($data->TGL_DETAIL_PURCHASE_RETURN)) : NULL),'TANGGAL_LPB'=>(strlen(trim($data->TANGGAL_LPB)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_LPB)) : NULL),'BATCH_NO'=>$data->BATCH_NO,'KODE_BARANG'=>$data->KODE_BARANG,'NAMA_BARANG'=>$data->NAMA_BARANG,'QUANTITY'=>$data->QUANTITY,'SATUAN'=>$data->SATUAN,'HARGA_SATUAN'=>$data->HARGA_SATUAN,'HARGA_TOTAL'=>$data->HARGA_TOTAL);
			 
			$this->db->where($pkey)->update('purchasertndet', $arrdatau);
			$last   = $data;
			
		}else{
			/*
			 * Data Not Exist
			 * 
			 * Process Insert
			 */
			 $tmp = substr($data->HARGA_SATUAN,3,strlen($data->HARGA_SATUAN));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->HARGA_SATUAN = $tmp;$tmp = substr($data->HARGA_TOTAL,3,strlen($data->HARGA_TOTAL));
			$tmp = str_replace('.','',$tmp);
			$tmp = str_replace(',','.',$tmp);
			$data->HARGA_TOTAL = $tmp;
			$arrdatac = array('NOMOR_PURCHASE_RETURN'=>$data->NOMOR_PURCHASE_RETURN,'TGL_DETAIL_PURCHASE_RETURN'=>(strlen(trim($data->TGL_DETAIL_PURCHASE_RETURN)) > 0 ? date('Y-m-d', strtotime($data->TGL_DETAIL_PURCHASE_RETURN)) : NULL),'NOMOR_LPB'=>$data->NOMOR_LPB,'TANGGAL_LPB'=>(strlen(trim($data->TANGGAL_LPB)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_LPB)) : NULL),'BATCH_NO'=>$data->BATCH_NO,'KODE_BARANG'=>$data->KODE_BARANG,'NAMA_BARANG'=>$data->NAMA_BARANG,'QUANTITY'=>$data->QUANTITY,'SATUAN'=>$data->SATUAN,'HARGA_SATUAN'=>$data->HARGA_SATUAN,'HARGA_TOTAL'=>$data->HARGA_TOTAL,'ID'=>$data->ID);
			 
			$this->db->insert('purchasertndet', $arrdatac);
			$last   = $this->db->where($pkey)->get('purchasertndet')->row();
			
		}
		
		$total  = $this->db->get('purchasertndet')->num_rows();
		
		$json   = array(
						"success"   => TRUE,
						"message"   => 'Data berhasil disimpan',
						'total'     => $total,
						"data"      => $last
		);
		
		return $json;
	}
	
	/**
	 * Fungsi	: delete
	 * 
	 * Untuk menghapus satu data
	 * 
	 * @param array $data
	 * @return json
	 */
	function delete($data){
		$pkey = array('NOMOR_PURCHASE_RETURN'=>$data->NOMOR_PURCHASE_RETURN,'NOMOR_LPB'=>$data->NOMOR_LPB);
		
		$this->db->where($pkey)->delete('purchasertndet');
		
		$total  = $this->db->get('purchasertndet')->num_rows();
		$last = $this->db->get('purchasertndet')->result();
		
		$json   = array(
						"success"   => TRUE,
						"message"   => 'Data berhasil dihapus',
						'total'     => $total,
						"data"      => $last
		);				
		return $json;
	}
}
?>